package com.cy.system.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cy.system.entity.Dept;
import com.cy.system.entity.Role;
import com.cy.system.entity.User;
import com.cy.system.entity.UserRole;
import com.cy.system.service.DeptService;
import com.cy.system.service.RoleService;
import com.cy.system.service.UserService;

@Controller
public class UserController {
	private Logger logger = Logger.getLogger(UserController.class);
	
	@Resource
	UserService userService;
	@Resource
	DeptService deptService;
	@Resource
	RoleService roleService;
	
	@RequestMapping("/delUser")
	public String delUser(int accountId) {
		userService.delUserByAccountId(accountId);
		return "system/user/userList";
	}
	
	@PostMapping("/editUser")    //此方法有问题，未知原因
	@ResponseBody                    
	public ModelAndView editUser(int accountId,ModelMap model) {
		logger.info("==> 编辑用户 accountId"+accountId );
		User user = userService.getUserByAccountId(accountId);
		List<Dept> deptList = deptService.getAllDept();
		List<Role> roleList = roleService.getAllRole();
		model.addAttribute("user",user);
		model.addAttribute("deptList",deptList);
		model.addAttribute("roleList",roleList);
		logger.info("==> roleList"+roleList);
		logger.info("==> 编辑User "+user);
		return new ModelAndView("/system/user/editpage2");
	}
	
	@GetMapping("/editUser2")
	public String editUser2(int accountId,ModelMap model) {
		logger.info("==> 编辑用户 accountId"+accountId );
		User user = userService.getUserByAccountId(accountId);
		List<Dept> deptList = deptService.getAllDept();
		List<Role> roleList = roleService.getAllRole();
		List<Integer> roleIds = userService.getUserRoleIds(user.getUserId());
		model.addAttribute("user",user);
		model.addAttribute("deptList",deptList);
		model.addAttribute("roleList",roleList);
		model.addAttribute("roleIds",roleIds);
		logger.info("==> roleIds"+roleIds);
		logger.info("==> 编辑User "+user);
		return "system/user/editPage2";
	}
	
	@RequestMapping("/updateUser")
	public String updateUser(User user,String roleIds,HttpSession session,Model model) {
		logger.info("==> 更新用户"+user );
		logger.info("==> roleIds"+roleIds);
		String[] roleIdArray = roleIds.split(",");
		List<UserRole> userRoleList = new ArrayList<UserRole>();
		for(String str : roleIdArray) {
			UserRole userRole = new UserRole(user.getUserId(),Integer.parseInt(str));
			userRoleList.add(userRole);
		};
		
		int resInt = userService.updateUser(user,session);
		resInt = userService.setUserRoleIds(userRoleList);
		if(resInt == 1) {
			model.addAttribute("msg","更新用户成功！");
		}else {
			model.addAttribute("msg","更新用户失败！");
		}
		
		return "system/user/userList";
	}

}
